import { TimerModel, ScheduleStateList, AddOrEditParams } from './type';
import {
  syncTimerTask,
  getLaunchOptionsSync,
  addTimer,
  updateTimer,
  removeTimer,
  updateTimerStatus,
} from '@ray-js/ray';

const {
  query: { deviceId, groupId },
} = getLaunchOptionsSync();
import _ from 'lodash';

const category = '';
const isAppPush = false;

export const getScheduleList = () => {
  return new Promise<TimerModel[]>((resolve, reject) => {
    syncTimerTask({
      deviceId, // groupId 和 deviceId 只能二选一。
      category,
      success: (result: { timers: TimerModel[] }) => {
        const { timers } = result;
        // store.dispatch(updateTimerList(timers));
        console.log('🐫 ~ getScheduleList ~ timers:', timers);
        resolve(timers);
      },
      fail: error => {
        console.log('error:', error);
        // reject(error);
        resolve([]);
      },
    });
  });
};

export const addSchedule = (params: AddOrEditParams) => {
  return new Promise<TimerModel[]>((resolve, reject) => {
    addTimer({
      deviceId,
      // 或
      groupId,
      category,
      timer: {
        isAppPush,
        time: params.time,
        loops: params.loops,
        aliasName: params.aliasName,
        dps: params.dps,
      },
      success: info => {
        return resolve(getScheduleList());
      },
      fail: error => {
        console.log('addTimer fail', error);
        reject(error);
      },
    });
  });
};

export const updateSchedule = (params: AddOrEditParams) => {
  return new Promise<TimerModel[]>((resolve, reject) => {
    updateTimer({
      deviceId,
      // 或
      groupId,
      timer: {
        timerId: params.timerId,
        isAppPush,
        time: params.time,
        loops: params.loops,
        aliasName: params.aliasName,
        dps: params.dps,
      },
      success: () => {
        // console.log('updateTimer success');
        return resolve(getScheduleList());
      },
      fail: error => {
        console.log('updateTimer fail', error);
      },
    });
  });
};

export const removeSchedule = (timerId: string) => {
  return new Promise<TimerModel[]>((resolve, reject) => {
    removeTimer({
      deviceId,
      // 或
      groupId,
      timerId,
      success: () => {
        return resolve(getScheduleList());
      },
      fail: error => {
        console.log('removeTimer fail', error);
      },
    });
  });
};

export const updateStatus = (params: { timerId: string; status: boolean }) => {
  return new Promise<TimerModel[]>((resolve, reject) => {
    updateTimerStatus({
      deviceId,
      // 或
      groupId,
      timerId: params.timerId,
      status: params.status,
      success: () => {
        // console.log('updateTimer success');
        return resolve(getScheduleList());
      },
      fail: error => {
        console.log('updateTimer fail', error);
      },
    });
  });
};

export const removeScheduleList = (timerIdList: string[]) => {
  return new Promise<TimerModel[]>((resolve, reject) => {
    timerIdList.forEach(timerId => {
      removeSchedule(timerId);
    });
    resolve(getScheduleList());
  });
};

export const removeAllSchedule = () => {
  return getScheduleList().then(timerList => {
    return Promise.all(timerList.map(timer => removeSchedule(timer.timerId)))
  });
};
